﻿using System;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

/// <summary>
/// Summary description for Infrastructure
/// </summary>
public class Infrastructure
{
    SqlConnection objSqlConnection;
    DataTable objReference, objReferenceMetaData, objApplicationLog;
    string sqlStr;

    public Infrastructure()
    {
        //
        // TODO: Add constructor logic here
        //
        objSqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["GenericBrokerEntities"].ConnectionString);
        objReference = new DataTable();
        objReferenceMetaData = new DataTable();
        objApplicationLog = new DataTable();
    }

    public static XmlDocument GetXMLDocumentFromUrl(string url)
    {
        HttpWebRequest objHttpWebRequest = null;
        HttpWebResponse objHttpWebResponse = null;
        XmlDocument objXMLDocument = null;
        XmlTextReader objXMLReader = null;

        try
        {
            //Create Request
            objHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            objHttpWebRequest.Method = "GET";
            objHttpWebRequest.ContentType = "text/xml; encoding='utf-8'";

            //Get Response
            objHttpWebResponse = (HttpWebResponse)objHttpWebRequest.GetResponse();

            //Now load the XML Document
            objXMLDocument = new XmlDocument();

            //Load response stream into XMLReader
            objXMLReader = new XmlTextReader(objHttpWebResponse.GetResponseStream());
            objXMLDocument.Load(objXMLReader);
        }
        catch (Exception ex)
        {
            throw new Exception("Error Occurred in Infrastructure.GetXMLDocumentFromUrl()", ex);
        }
        finally
        {
            objHttpWebRequest = null;
            objHttpWebResponse = null;
            objXMLReader = null;
        }
        return objXMLDocument;
    }

    private byte[] FileToByteArray(string fileName)
    {
        byte[] buff = null;
        FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fs);
        long numBytes = new FileInfo(fileName).Length;
        buff = br.ReadBytes((int)numBytes);
        fs.Close();
        return buff;
    }

    public static string GetIP4Address()
    {
        string IP4Address = string.Empty;
        foreach (IPAddress IPA in Dns.GetHostAddresses(HttpContext.Current.Request.UserHostAddress))
        {
            if (IPA.AddressFamily.ToString() == "InterNetwork")
            {
                IP4Address = IPA.ToString();
                break;
            }
        }

        if (IP4Address != string.Empty)
        {
            return IP4Address;
        }

        foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
        {
            if (IPA.AddressFamily.ToString() == "InterNetwork")
            {
                IP4Address = IPA.ToString();
                break;
            }
        }
        return IP4Address;
    }

    public static string StripXmlHeader(string data)
    {
        if (data == null) data = "";
        string result = data;

        try
        {
            if (data.IndexOf("?>") > -1)
            {
                result = data.Substring(data.IndexOf("?>") + 2, data.Length - data.IndexOf("?>") - 2);
            }
        }
        catch { }

        return result;
    }
}
